
package com.jf.cloud.live.controller.multishop;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.common.util.CharUtil;
import com.jf.cloud.live.param.LiveUsableNumParam;
import com.jf.cloud.live.constant.LiveConstant;
import com.jf.cloud.live.constant.LiveInterfaceType;
import com.jf.cloud.live.constant.LiveRoomStatusType;
import com.jf.cloud.live.dto.LiveRoomDTO;
import com.jf.cloud.live.model.LiveRoom;
import com.jf.cloud.live.model.LiveRoomBack;
import com.jf.cloud.live.service.LiveLogService;
import com.jf.cloud.live.service.LiveRoomBackService;
import com.jf.cloud.live.service.LiveRoomProdService;
import com.jf.cloud.live.service.LiveRoomService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;


/**
 * 直播间
 *
 * @author zz
 * @date 2022-08-17 17:09:06
 */
@RestController("multishopLiveRoomController")
@RequestMapping("/m/live_room")
@Tag(name = "商家端直播间接口")
public class LiveRoomController {

    @Autowired
    private LiveRoomService liveRoomService;

    @Autowired
    private  LiveRoomBackService liveRoomBackService;

    @Autowired
    private LiveLogService liveLogService;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private LiveRoomProdService liveRoomProdService;



    @GetMapping("/page")
    @Operation(summary = "获取直播间列表" , description = "分页获取直播间列表")
    public ServerResponseEntity<PageVO<LiveRoom>> page(@Valid PageDTO pageDTO, @ParameterObject LiveRoom liveRoom) {
        liveRoom.setShopId(AuthUserContext.get().getTenantId());
        PageVO<LiveRoom> liveRoomPage = liveRoomService.page(pageDTO, liveRoom);
        return ServerResponseEntity.success(liveRoomPage);
    }

    @GetMapping("/info/{id}")
    @Operation(summary = "获取直播间" , description = "根据id获取直播间")
    @Parameter(name = "id", description = "直播间id" )
    public ServerResponseEntity<LiveRoom> getById(@ParameterObject @PathVariable("id") Long id) {
        LiveRoom liveRoomDb = liveRoomService.getById(id);
        if (!Objects.equals(AuthUserContext.get().getTenantId(), liveRoomDb.getShopId())) {
            return ServerResponseEntity.fail(ResponseEnum.UNAUTHORIZED);
        }
        return ServerResponseEntity.success(liveRoomDb);
    }


    @GetMapping("/getPlayBack/{id}")
    @Operation(summary = "通过id查询直播间回放信息")
    @Parameter(name = "id", description = "直播间id", required = true)
    public ServerResponseEntity<LiveRoomBack> getPlayBackByRoomId(@ParameterObject @PathVariable Long id) {
        LiveRoomBack liveRoomBack = liveRoomBackService.getByBackId(id);
        return ServerResponseEntity.success(liveRoomBack);
    }

    @PostMapping
    @Operation(summary = "保存直播间" , description = "保存直播间")
    public ServerResponseEntity<Void> save(@Valid @RequestBody LiveRoom liveRoom) {
        if (CharUtil.length(liveRoom.getName()) < LiveConstant.ROOM_NAME_MIN_LENGTH || CharUtil.length(liveRoom.getName()) > LiveConstant.ROOM_NAME_MAX_LENGTH) {
            // 直播间名称长度非法,最短3个汉字，最长17个汉字！
            throw new LuckException("直播间名称长度非法,最短3个汉字，最长17个汉字！");
        }
        if (CharUtil.length(liveRoom.getAnchorName()) < LiveConstant.USER_NAME_MIN_LENGTH || CharUtil.length(liveRoom.getAnchorName()) > LiveConstant.USER_NAME_MAX_LENGTH) {
            // 主播昵称长度非法，最短2个汉字，最长15个汉字
            throw new LuckException("主播昵称长度非法，最短2个汉字，最长15个汉字");
        }
        liveRoom.setShopId(AuthUserContext.get().getTenantId());
        liveRoom.setApplyTime(new Date());
        liveRoom.setLiveStatus(LiveRoomStatusType.NO_START.value());
        liveRoom.setId(null);
        liveRoom.setUpdateTime(new Date());
        liveRoom.setRoomTop(Objects.nonNull(liveRoom.getRoomTop())? liveRoom.getRoomTop() : 0);
        liveRoomService.saveLiveRoom(liveRoom);
        return ServerResponseEntity.success();
    }

    @PostMapping("/addLiveRoomProd")
    @Operation(summary = "新增直播间商品")
    public ServerResponseEntity<Boolean> addLiveRoomProd(@RequestBody @Valid LiveRoom liveRoom) throws WxErrorException {
        liveRoom.setShopId(AuthUserContext.get().getTenantId());
        liveRoomProdService.saveBatchAndRoomId(liveRoom);
        return ServerResponseEntity.success();
    }

    @GetMapping("/getAddRoomNum")
    @Operation(summary = "查询商家今日剩余可用添加直播间次数")
    public ServerResponseEntity<LiveUsableNumParam> getAddRoomNum() {
        LiveUsableNumParam liveNum = liveLogService.getLiveNum(AuthUserContext.get().getTenantId(), LiveInterfaceType.CREATE_ROOM);
        LiveUsableNumParam deleteLiveNum = liveLogService.getLiveNum(AuthUserContext.get().getTenantId(), LiveInterfaceType.REMOVE_LIVE_ROOM);
        liveNum.setDeleteShopNum(deleteLiveNum.getShopNum());
        liveNum.setDeleteTotalNum(deleteLiveNum.getTotalNum());
        return ServerResponseEntity.success(liveNum);
    }

    @PutMapping
    @Operation(summary = "更新直播间" , description = "更新直播间")
    public ServerResponseEntity<Void> update(@Valid @RequestBody LiveRoomDTO liveRoomDTO) {
        LiveRoom liveRoom = mapperFacade.map(liveRoomDTO, LiveRoom.class);
        liveRoomService.update(liveRoom);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除直播间" , description = "根据直播间id删除直播间")
    public ServerResponseEntity<Boolean> delete(@RequestParam Long id) throws WxErrorException {
        return ServerResponseEntity.success(liveRoomService.removeRoomById(id, AuthUserContext.get().getTenantId()));
    }
}
